//
//  SEGOldRegisterViewModel.m
//  Commom
//
//  Created by Clavis on 2020/6/28.
//

#import "SEGOldRegisterViewModel.h"
#import "SEGRequestManager.h"

@interface SEGOldRegisterViewModel ()

@property (nonatomic, strong) SEGRequestManager *oldRegisterManager;

@end

@implementation SEGOldRegisterViewModel

/// 获取图形验证码
- (RACSignal *)refreshImgSignal {
    if (!_refreshImgSignal) {
        @weakify(self);
        _refreshImgSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [self.oldRegisterManager requestSecurityRefreshCheckCodeWithFinished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
                @strongify(self);
                if (isSuccessed) {
                    if ([self.oldRegisterManager.seg_segtelcaptchasbyregistermodel.imgCode length] > 0) {
                        NSData *decodedImageData = [[NSData alloc] initWithBase64EncodedString:self.oldRegisterManager.seg_segtelcaptchasbyregistermodel.imgCode options:NSDataBase64DecodingIgnoreUnknownCharacters];
                        UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
                        [subscriber sendNext:decodedImage];
                        [subscriber sendCompleted];
                    } else {
                        [subscriber sendNext:nil];
                        [subscriber sendCompleted];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }
    return _refreshImgSignal;
}

/// 获取短信验证码
- (RACCommand *)getMessageCommand {
    if (!_getMessageCommand) {
        @weakify(self);
        _getMessageCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                RACTupleUnpack(NSString *tel, NSString *imgCode) = input;
                [self.oldRegisterManager requestVerCodeForRegisterByRegister:tel ?: @"" imgCode:imgCode ?: @"" randomToken:self.oldRegisterManager.seg_segtelcaptchasbyregistermodel.randomToken Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                    if (isSuccessed) {
                        [subscriber sendNext:nil];
                        [subscriber sendCompleted];
                    }
                    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _getMessageCommand;
}

/// 获取语音验证码
- (RACCommand *)getSoundMessageCommand {
    if (!_getSoundMessageCommand) {
        @weakify(self);
        _getSoundMessageCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                RACTupleUnpack(NSString *tel, NSString *imgCode, NSString *type) = input;
                [self.oldRegisterManager requestGetSoundCodeByTel:tel imgCode:imgCode ?: @"" randomToken:self.oldRegisterManager.seg_segtelcaptchasbyregistermodel.randomToken Type:type ?: @"" Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                    if (isSuccessed) {
                        [subscriber sendNext:errorMessage];
                        [subscriber sendCompleted];
                    }
                    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _getSoundMessageCommand;
}

/// 校验短信验证码
- (RACCommand *)messageValidateCommand {
    if (!_messageValidateCommand) {
        @weakify(self);
        _messageValidateCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                RACTupleUnpack(NSString *tel, NSString *smsCode) = input;
                [self.oldRegisterManager requestValidateCaptchasByTel:tel?:@"" smsCode:smsCode?:@"" password:nil Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    if (isSuccessed) {
                        [subscriber sendNext:self.oldRegisterManager.seg_segtelcaptchasbyregistermodel.randomToken];
                        [subscriber sendCompleted];
                    }
                    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _messageValidateCommand;
}

- (SEGRequestManager *)oldRegisterManager {
    if (!_oldRegisterManager) {
        _oldRegisterManager = [SEGRequestManager new];
    }
    return _oldRegisterManager;
}

@end
